<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.sid" clearable filterable class="filter-item" placeholder="选择区服">
        <el-option
          v-for="(item, index) in servers"
          :key="index"
          :label="item.name + '(' + item.sid + ')'"
          :value="item.sid"
        />
      </el-select>
      <el-input v-model="listQuery.keyword" class="filter-item" style="width: 150px;" placeholder="联盟ID/联盟名字" />
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="670">
      <el-table-column label="服务器" prop="sname" width="80px" align="center"></el-table-column>
      <el-table-column label="联盟ID" prop="union_id" width="100px" align="center"></el-table-column>
      <el-table-column label="名称" prop="name" width="120px" align="center"></el-table-column>
      <el-table-column label="宣言" prop="declaration" width="400px" align="center"></el-table-column>
      <el-table-column label="等级" prop="level" width="60px" align="center"></el-table-column>
      <el-table-column label="繁荣度" prop="prosperity" width="80px" align="center"></el-table-column>
      <el-table-column label="联盟人数" prop="num" width="80px" align="center"></el-table-column>
      <el-table-column label="会长ID" prop="master_id" width="120px" align="center"></el-table-column>
      <el-table-column label="会长名称" prop="master_name" width="100px" align="center"></el-table-column>
      <el-table-column width="255px" align="center" label="操作">
        <template slot-scope="{row}">
          <el-button v-if="checkPermission(permissionRoles.union_master)" type="text" size="small" title="指定会长" @click="handleMaster(row)">指定会长</el-button>
          <el-button v-if="checkPermission(permissionRoles.union_rename)" type="text" size="small" title="修改名称" @click="handleName(row)">修改名称</el-button>
          <el-button v-if="checkPermission(permissionRoles.union_declaration)" type="text" size="small" title="重置宣言" @click="handleDeclaration(row)">重置宣言</el-button>
          <el-button v-if="checkPermission(permissionRoles.union_member)" type="text" size="small" title="成员列表" @click="showMembers(row.ms)">成员列表</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog :visible.sync="dialogVisible" title="指定会长" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" label-width="100px" label-position="left">
        <el-form-item label="现会长">
          <el-input v-model="dataForm.original_content" :disabled="true" class="defaut-input" placeholder="现会长" />
        </el-form-item>
        <el-form-item label="联盟成员">
          <el-select v-model="dataForm.new_content" clearable class="filter-item" style="width: 220px;" placeholder="联盟成员">
            <el-option
              v-for="(item, index) in members"
              :key="index"
              :label="item.show_info"
              :value="item.actor_id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="mDialogVisible" title="修改名称" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" label-width="100px" label-position="left">
        <el-form-item label="现名称">
          <el-input v-model="dataForm.original_content" :disabled="true" class="defaut-input" placeholder="原会长" />
        </el-form-item>
        <el-form-item label="新名称">
          <el-input v-model="dataForm.new_content" class="defaut-input" placeholder="请输入新名称" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="mDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dDialogVisible" title="重置宣言" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" label-width="100px" label-position="left">
        <el-form-item label="帮派名称">
          <el-input v-model="dataForm.union_name" :disabled="true" class="defaut-input" placeholder="帮派名称" />
        </el-form-item>
        <el-form-item label="现宣言">
          <el-input v-model="dataForm.original_content" :disabled="true" class="defaut-input" placeholder="原宣言" />
        </el-form-item>
        <el-form-item label="禁止修改时间">
          <el-input v-model="dataForm.new_content" class="defaut-input" placeholder="请输入禁止修改时间" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="membersDialogVisible" title="联盟成员列表" :close-on-click-modal="false">
      <el-table ref="table" :data="members" border stripe style="width: 100%;" height="485">
        <el-table-column label="角色ID" prop="actor_id" width="110px" align="center"></el-table-column>
        <el-table-column label="角色名" prop="actor_name" width="100px" align="center"></el-table-column>
        <el-table-column label="联盟职位" prop="job" width="100px" align="center"></el-table-column>
        <el-table-column label="角色等级" prop="level" width="80px" align="center"></el-table-column>
        <el-table-column label="角色战力" prop="power" width="80px" align="center"></el-table-column>
        <el-table-column label="在线状态" width="70" align="center">
          <template slot-scope="{row}">
            <el-tag v-if="row.is_online === 1" type="success">在线</el-tag>
            <el-tag v-else type="danger">离线</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import checkPermission from '@/utils/permission'
import { getUnionList, handleUnion } from '@/api/gm'
import { getServerList } from '@/api/ops'

const defaultItem = {
  id: '',
  sid: '',
  union_id: '',
  union_name: '',
  type: '',
  original_content: '',
  new_content: ''
}
export default {
  name: 'UnionList',
  components: { Pagination },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      dataForm: Object.assign({}, defaultItem),
      list: [],
      members: [],
      servers: [],
      total: 0,
      listLoading: false,
      listQuery: {
        sid: '',
        keyword: '',
        page: 1,
        limit: 200
      },
      dialogVisible: false,
      membersDialogVisible: false,
      dDialogVisible: false,
      mDialogVisible: false
    }
  },
  created() {
    this.getList()
    this.getServerList()
  },
  methods: {
    checkPermission,
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getUnionList(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
    },
    handleFilter() {
      this.getList()
    },
    handleMaster(row) {
      this.dataForm = Object.assign({}, defaultItem)
      this.dataForm.type = 1
      this.dataForm.union_id = row.union_id
      this.dataForm.union_name = row.name
      this.dataForm.sid = row.sid
      this.members = row.ms
      this.dataForm.original_content = row.master_id
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleName(row) {
      this.dataForm = Object.assign({}, defaultItem)
      this.dataForm.type = 2
      this.dataForm.union_id = row.union_id
      this.dataForm.union_name = row.name
      this.dataForm.sid = row.sid
      this.dataForm.original_content = row.name
      this.mDialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDeclaration(row) {
      this.dataForm = Object.assign({}, defaultItem)
      this.dataForm.type = 3
      this.dataForm.union_id = row.union_id
      this.dataForm.sid = row.sid
      this.dataForm.union_name = row.name
      this.dataForm.original_content = row.declaration
      this.dataForm.new_content = 12
      this.dDialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleUnion(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.dDialogVisible = false
              this.mDialogVisible = false
              this.membersDialogVisible = false
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    },
    async getServerList(params) {
      const query = params
      const res = await getServerList(query)
      this.servers = res.data.items
      this.loading = false
    },
    showMembers(members) {
      this.members = members
      this.membersDialogVisible = true
    }
  }
}

</script>
